﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using RssReader.Core.BL;

namespace RssReader.Core.UI
{
    public class ChangePasswordControl:BaseControl
    {
        #region Private Properties
        private TextBox _tbOldPassword;
        private TextBox _tbNewPassword;
        private TextBox _tbConfirmPassword;
        private LinkButton _lbSubmit;
        private LinkButton _lbCancel;
        #endregion

        #region Public Properties

        #endregion

        #region Constructors

        #endregion

        #region Private Methods
        protected override void AttachChildControls()
        {
            _tbOldPassword = this.FindControl("tbOldPassword") as TextBox;
            _tbNewPassword = this.FindControl("tbNewPassword") as TextBox;
            _tbConfirmPassword = this.FindControl("tbConfirmPassword") as TextBox;
            _lbSubmit = this.FindControl("lbSubmit") as LinkButton;
            _lbCancel = this.FindControl("lbCancel") as LinkButton;

            if (_lbSubmit != null)
            {
                _lbSubmit.Click += new EventHandler(_lbSubmit_Click);
            }

            if (_lbCancel != null)
            {
                _lbCancel.Click += new EventHandler(_lbCancel_Click);

            }
        }
        protected void _lbSubmit_Click(object sender, EventArgs e)
        {

            CheckChange();
            
        }
        private void CheckChange()//check passwordControl and change password
        {
            string oldPassword = _tbOldPassword.Text;
            string newPassword = _tbNewPassword.Text;
            string confirmPassword = _tbConfirmPassword.Text;
            if (Validation.Instance.IsEmpty(oldPassword))
            {
                this.ThrowError("OldPassword is empty");
            }
            else if (Validation.Instance.IsEmpty(newPassword))
            {
                this.ThrowError("NewPassword is empty");
            }
            else if (Validation.Instance.IsEmpty(confirmPassword))
            {
                this.ThrowError("ConfirmPassword is empty");
            }
            else if (newPassword == confirmPassword)
            {
                confirmPassword = Global.Utility.EncryptString(confirmPassword);
                oldPassword = Global.Utility.EncryptString(oldPassword);
               
                if (this.CurrentUser.Password == oldPassword)
                {
                    
                    this.CurrentUser.Password = confirmPassword;
                    
                    this.CurrentPage.Response.Redirect("RssList.aspx");
                }
                else
                {
                    this.ThrowError("OldPassword is Mistake!!!");
                }
            }
            else
            {
                this.ThrowError("NewPassword and ConfirmPassword is Different");
            }
        }
        protected void _lbCancel_Click(object sender, EventArgs e)
        {
            this.CurrentPage.Response.Redirect("RssList.aspx");
            
        }
        
        #endregion

       #region Public Methods

       #endregion
    }
}
